<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/include/taglib.jsp"%>
<html>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no">
		<meta charset="UTF-8">
		<title>季发宝_共富网_Gofull.com.cn</title>
		<script src="/v3/js/jquery-1.10.2.min.js" type="text/javascript"></script>
		<script type="text/javascript" src="/v3/js/event/global-1.1.0.min.js"></script>
		<link type="text/css" rel="stylesheet" href="/v3/css/wstyle.css">
		<link type="text/css" rel="stylesheet" href="/v3/css/account.css">
		<script type="text/javascript" src="/v3/js/showts.js"></script>
	</head>
	<body>
		<div id="wrap">
			<ul class="proItem">
				<s:if test="#request.yebMap.productStatus == 1">
				<li onclick="location.href='/loanYebDetailInvest.do?pid=${yebMap.pid }'">
					余额宝
					<span class="proRate">${yebAnnualRate }%</span>
				</li>
				</s:if>
				<s:else>
				<li class="disable">
					余额宝
					<span class="proRate">已售完</span>
				</li>
				</s:else>
				<s:if test="#request.ylbMap.productStatus == 1">
				<li onclick="location.href='/loanYlbDetailInvest.do?pid=${ylbMap.pid }'">
					月利宝
					<span class="proRate">${ylbMap.productAnnualRate }%</span>
				</li>
				</s:if>
				<s:else>
				<li class="disable">
					月利宝
					<span class="proRate">已售完</span>
				</li>
				</s:else>
				<s:if test="#request.wybMap.borrowStatus != 2">
				<li class="disable">
					稳盈宝
					<span class="proRate">已售完</span>
				</li>
				</s:if>
				<s:else>
				<li onclick="location.href='/loanWybDetailInvest.do'">
					稳盈宝
					<span class="proRate">${wybMap.annualRate }%</span>
				</li>
				</s:else>
				<s:if test="#request.debtMap.debtStatus != 2">
				<li class="disable">
					债权
					<span class="proRate">已售完</span>
				</li>
				</s:if>
				<s:else>
				<li onclick="location.href='/loanDebtDetailInvest.do'">
					债权
					<span class="proRate">${debtMap.realAnnualRate }%</span>
				</li>
				</s:else>
				
				<li onclick="location.href='/loanYlbDetailInvest.do?pid=${ylbMap.pid }'">
					月利宝
					<span class="proRate">${ylbMap.productAnnualRate }%</span>
				</li>
				<li onclick="location.href='/loanWybDetailInvest.do'">
					稳盈宝
					<span class="proRate">${wybMap.annualRate }%</span>
				</li>
				<li onclick="location.href='/loanDebtDetailInvest.do'">
					债权
					<span class="proRate">${debtMap.realAnnualRate }%</span>
				</li>
			</ul>
			<div class="itemInfo">
				<p class="itemTo">年化收益率</p>
				<img src="/v3/images/circle_bg2.png"/>
				<p class="itemRate" id="q_hieght">${jfbMap.productAnnualRate }%</span></p>
				<p class="itemIf"><a href="wchatJfbDetail.do?pid=${jfbMap.pid }">查看详情>></a></p>
			</div>
			<div class="reMoney">可售金额：￥${productDetailMap.remain}</div>
			<s:if test="#request.availableVoucherList.size>0">
			<div class="useVoucher">
							<div class="yCheckbox">
							<span class="ico" id="novice-chk"></span>
							</div>
							<select id="voucherSelect">
							<s:iterator value="#request.availableVoucherList" var="availableVoucher" status="s">
							<option value="${availableVoucher.id }" id="voucher${availableVoucher.id }">
								投资抵用券 ￥${availableVoucher.remainMoney }</option>
							</s:iterator>
							</select>
							<s:iterator value="#request.availableVoucherList" var="availableVoucher" status="s">
							<span id="availableVoucherAmount" class="availableVoucherAmount"
								voucherid="${availableVoucher.id}" leftamount="${availableVoucher.remainMoney }" 
								voucherType="${availableVoucher.voucherType}" discountratio="${availableVoucher.exchangeRate}" 
								typetitle="投资抵用券"></span>
						</s:iterator>
						</div>
			</s:if>
			<div class="money-input">
				<div class="login_input inv_input">
				<div class="r_line">
				<i class="icon-inMoney"></i>
				</div>
				<input type="hidden" id="realnameFlag" value="${session.user.isBindIdNo}">
				<div class="in_put"><input onkeyup="calAvailableVoucherAmount();" placeholder="购买金额/最低50元" id="inputAmount" name="inputAmount" maxlength="17" type="tel" onkeyup="calAvailableVoucherAmount();" ></div>
				</div>
				<div class="box-bottom"><img src="/v3/images/box_icon.jpg"/></div>
			</div>
			<div class="Nxt-btn"><a id="investBt" href="javascript:invest();" >确认购买</a>
			<a href="/wchatRealNameAuthen.do" class="btn" id="realNameBt" style="display: none;">立即实名认证</a></div>
			<p class="inv-note">每个工作日&nbsp;10:00、14:00、17:00&nbsp;更新总额度</p>
		</div>
		<jsp:include page="/include/v3/footer.jsp"></jsp:include>
		
		<script type="text/javascript">
var product = {
		aunualInterestRate : '${productDetailMap.productAnnualRate}',
		remain : '${productDetailMap.remain}',
		id : '${productDetailMap.pid}'
	};
var usableSum = '${accmountStatisMap.usableSum}';
$(function(){
Util.CustomCheckbox($('.yCheckbox'));
  $('#agreeContract').click(function(){
    if ($('#agreeContract').attr("checked") == "checked") {
      $('#agreeContract').removeAttr("checked");
    } else {
      $('#agreeContract').attr("checked", "checked");
    }
  });
  $('#novice-chk').click(function(){
	    if ($('#novice-chk').attr("checked") == "checked") {
	      $('#novice-chk').removeAttr("checked");
	    } else {
	      $('#novice-chk').attr("checked", "checked");
	    }
 }); 
});
/**
//刷新验证码
function coderefresh() {
    var verifyCode = $("#verifyCodeImg");
    var now = new Date();
    verifyCode.attr("src",'I4008lOve893gOfUll983/imageCode.do?pageId=jfbWchatInvest&d=' + now.getTime());
}
**/
//投资检查
function investCheck() {
	var errorTip = $('#errorTip');
	var cash = Number(usableSum);
    var investAmountInput = $("#inputAmount");
    var investAmount = investAmountInput.val();
    var maxInvestAmount = Number(product.remain);
    var investableCash = Number(usableSum);
    var availableCouponAmount = Number($("#availableCouponAmount").html());
    var reg = /^\d+$/;
    if(!reg.test(investAmount)){
    	showAlert('请输入50~'+maxInvestAmount+'的整数');
        return false;
    }
    if(isNaN(investAmount*1) || investAmount*1<=0){
    	showAlert('请输入正确的金额');
        return false;
    }
   
    if(investAmount > maxInvestAmount){
    	showAlert('投资额不能大于剩余可投额度');
        return false;
    }
    
    if(investAmount > cash){
    	showAlert('您的可用余额不足');
    	$("#sure-bt").html("<a href='/quickpayment.do'>立即充值</a>");
        return false;
    }
    
    if(investAmount < 50.00){
    	showAlert('最小投资金额为'+50.00);
        return false;
    }
    
    if(((maxInvestAmount-investAmount)<50)&&(maxInvestAmount-investAmount)!=0){
        var requiredLeftAmount = Number(maxInvestAmount-50);
        if(requiredLeftAmount > 50){
        	showAlert('可投金额为'+maxInvestAmount+'或50~'+requiredLeftAmount);
        }else if(requiredLeftAmount <= 50){
        	showAlert('可投金额为'+maxInvestAmount);
        }
        return false;
    }
	/**
    var verifyCodeInput = $("#authCode");
    var verifyCode = verifyCodeInput.val();
    if(verifyCode == ''){
    	errorTip.html('请输入验证码');
        return false;
    }

    if($('#agreeContract').attr("checked") != "checked"){
    	errorTip.html('请同意共富网投资管理协议');
        return false;
    }**/
    
    //未实名认证，不能投资
	if($("#realnameFlag").val() == "0"){
		showAlert('未实名认证，不能投资');
		$('#realNameBt').show();
		$('#investBt').hide();
        return false;
    }
    return true;
}
//投资月息通或新手标
function invest() {
  if(!investCheck()){
    return;
  };
  
  var voucherId = 0;
  if($('#novice-chk').attr("checked") == "checked"){
	voucherId = $("#voucherSelect").val();
  }
  
  var errorTip = $('#errorTip');
  var investAmount = $("#inputAmount").val();
  var verifyCode = $("#authCode").val();
  var profitDisposal = 0;
  var param = {};
  param['paramMap.pid'] = product.id;
  param["paramMap.pageId"] = "jfbWchatInvest";
  param["paramMap.code"] = verifyCode;
  param['paramMap.amount'] = investAmount;
  param['paramMap.profitDisposal'] = profitDisposal;
  param['paramMap.voucherId'] = voucherId;
  
  $.ajax({
          dataType: 'json',
          url: "/jfbFinanceInvest.do",
          data: param,
          type: 'POST',
          beforeSend: function() {
              $("#investBt").attr("href","javascript:;").html("正在加入...").addClass("gbtn-disabled");
          },
          success: function(data) {
              if(data.result == 'success'){
            	  $("#inputAmount").val("");
            	  window.location.href="/investSuccess.do";
                 // window.location.href="/wchatJfbDetail.do?pid="+${productDetailMap.pid};
              }else{
                  if(data.msg == '验证码错误'){
                      coderefresh();
                      showAlert("验证码错误");
                      $("#investBt").attr("href","javascript:invest();").html("我要购买").removeClass("gbtn-disabled");
                  }else{
                	  coderefresh();
                	  errorTip.html(data.msg);
                      $("#investBt").attr("href","javascript:invest();").html("我要购买").removeClass("gbtn-disabled");
                  }
              }
          },
          error: function() {
        	  coderefresh();
        	  showAlert("投资失败，请刷新页面或稍后再试");
          }
      });

}
//计算可使用红包金额
function calAvailableVoucherAmount(){
    var inputAmount = Number($("#inputAmount").val());
    var availableVoucherAmount = $(".availableVoucherAmount");
    for(var i=0,len=availableVoucherAmount.length;i<len;i++){
        var leftAmount = Number($(availableVoucherAmount[i]).attr("leftamount"));
        var voucherType = Number($(availableVoucherAmount[i]).attr("voucherType"));
        var discountRatio = Number($(availableVoucherAmount[i]).attr("discountratio"));
        var discountAmount = Number($(availableVoucherAmount[i]).attr("discountamount"));
        var voucherId = $(availableVoucherAmount[i]).attr("voucherid");
        var typeTitle = $(availableVoucherAmount[i]).attr("typetitle");
        var useVoucher=$('#voucher' + voucherId);
        if(voucherType == 1){
            var availableAmount = inputAmount*discountRatio;
            if(availableAmount > leftAmount){
              availableAmount = leftAmount;
            }
        }
       
        if(isNaN(availableAmount)){
            availableAmount = 0;
        }
        useVoucher.html(typeTitle + "￥" + availableAmount.toFixed(2));
        useVoucher.attr("useVoucherAmount", availableAmount.toFixed(2));
    }
}

window.onload=function(){
	var money = 0;
	if(${accmountStatisMap.usableSum}>${productDetailMap.remain}){
			money = Math.floor(${productDetailMap.remain});
		}else if(${accmountStatisMap.usableSum}<${productDetailMap.remain}){
			money = Math.floor(${accmountStatisMap.usableSum});
		}else{
			money = Math.floor(${accmountStatisMap.usableSum});
		}
	if(${accmountStatisMap.usableSum}<50){
		$("#inputAmount").attr("placeholder","50起投，余额不足请充值");
	}else{
		$("#inputAmount").attr("placeholder","可购买金额50~"+money+"元");
	}
}
</script>


	</body>
</html>
